<?php
// $Id: index.php,v 1.21 2004/09/01 17:48:07 hthouzard Exp $ 
//  ------------------------------------------------------------------------ //
//                XOOPS - PHP Content Management System                      //
//                    Copyright (c) 2000 XOOPS.org                           //
//                       <http://www.xoops.org/>                             //
// ------------------------------------------------------------------------- //
//  This program is free software; you can redistribute it and/or modify     //
//  it under the terms of the GNU General Public License as published by     //
//  the Free Software Foundation; either version 2 of the License, or        //
//  (at your option) any later version.                                      //
//                                                                           //
//  You may not change or alter any portion of this comment or credits       //
//  of supporting developers from this source code or any supporting         //
//  source code which is considered copyrighted (c) material of the          //
//  original comment or credit authors.                                      //
//                                                                           //
//  This program is distributed in the hope that it will be useful,          //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
//  GNU General Public License for more details.                             //
//                                                                           //
//  You should have received a copy of the GNU General Public License        //
//  along with this program; if not, write to the Free Software              //
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
//  ------------------------------------------------------------------------ //


//####################################################
//############### INCLUDES AND MOD VARIABLES #########
//####################################################
include_once '../../mainfile.php';
include( XOOPS_ROOT_PATH . "/header.php" ) ;


if(isset($_POST['op'])) {
 $op=$_POST['op'];
}else if(isset($_GET['op'])) {
  $op=$_GET['op'];
}

//$DB COnnection
if($xoopsModuleConfig['enableSyndication'] == 1){
	$link = mysql_connect($xoopsModuleConfig['syndicationDB_Host'], $xoopsModuleConfig['syndicationDB_User'], $xoopsModuleConfig['syndicationDB_Passaword']) or ($error = mysql_error());
}

//####################################################
//############### NEWS STORY FUNCTIONS ###############
//####################################################
	  function getSoundFile($sound){
	    global $xoopsModule;
	  	if(!ereg('http', $sound)){
			return XOOPS_URL_NON_SSL."/modules/local_news/media/audio/".$sound;
		}else{
			return $sound;
		}
	  }//end function 
	  
	  
	  function getContactFormURL($path){
	  	global $xoopsModule;
	  	if(!ereg('http', $path)){
			return XOOPS_URL_NON_SSL;
		}else{
			return substr($path,0,15);
		}
	  	
	  }
	  
	  function getImageUrl($image,$TN=false){
	    global $xoopsModule;
		if(!ereg('http', $image) && $TN==false){
			return XOOPS_URL_NON_SSL."/modules/local_news/media/pictures/".$image;
		}elseif($TN==false){
			return $image;
		}elseif(!ereg('http', $image) && $TN==true){
			return XOOPS_URL_NON_SSL."/modules/local_news/media/pictures/".substr_replace($image, '-TN.jpg', -4);
		}else{
			return substr_replace($image, '-TN.jpg', -4);
		}
	  }
	  
	  
	  function getCopyright($date,$copyright){
	  	if($copyright != ""){
	  		return "&copy; Copyright ".date("Y",$date).", ".$copyright;
	  	}else{
	  		return "&nbsp;";
	  	}
	  		  	
	  }
	  
	  
//####################################################
//############### PAGE NAVIGATION ####################
//####################################################	  
if(!isset($op)){
	$op = 'default';
}
switch ($op)
{
	case "sideBlock":
	if (!isset($_GET['syndicated'])){
	$_GET['syndicated'] = "";
	}
	fetchStory($_GET['ID'],$_GET['syndicated']);
	break;	
	
	case "centerBlock":
	if (!isset($_GET['syndicated'])){
	$_GET['syndicated'] = "";
	}
	fetchStory($_GET['ID'],$_GET['syndicated']);
	break;
	
	case "se"://get paginated stories from REGIONAL DB
	fetchMultipleStories('se');
	break;
	
	default://get paginated stories from LOCAL DB
	$_GET['op'] = 'default';
	fetchMultipleStories('local');
	break;
}


//####################################################
//############### JAVASCRIPT #########################
//####################################################
//set global js variables
echo "\n<script language= 'JavaScript' type='text/JavaScript'>
<!--
	xoopsURL = '".XOOPS_URL."';
-->
</script>";



//get main news mosdule js
echo '<script type="text/javascript" src="js/newsIndex.js"></script>';






//####################################################################################
//############### FETCH MULTIPLE STORIES FROM INDEX PAGE #############################
//####################################################################################
function fetchMultipleStories($DB){


  global $xoopsDB, $xoopsConfig, $xoopsModule, $myts, $xoopsUser, $xoopsModuleConfig, $link;
  $myts =& MyTextSanitizer::getInstance();

if(!isset($DB)){
	$DB = 'local';
}

	 //GET INFO FROM EITHER LOCAL OR REGIONAL DB
if(isset($DB) && $DB=='local' && $_SESSION['DB'] != $DB){
			//-------------------------------------------------------------------
			$result = $xoopsDB->query( "SELECT * FROM ".$xoopsDB->prefix("local_news_stories")." WHERE soundfile != '' ORDER BY publish_date desc,ID DESC LIMIT ".$xoopsModuleConfig['numOfStories2Show']."" );
			if ( $result ) {
				 $_SESSION['myrow'] = array();	
			while ($myrow = $xoopsDB->fetchArray($result) ) {
					$_SESSION['myrow'][] = $myrow;					
				}//end loop
			}	
			$_SESSION['headerTitle'] = strtoupper($xoopsModuleConfig['stationLetters']) . " Local News";
			$_SESSION['DB'] = $DB;
			//-------------------------------------------------------------------
	}elseif($DB=='se' && $_SESSION['DB'] != $DB){//get from Syndicated DB
			//-------------------------------------------------------------------
			$Query = "SELECT * FROM CA_SE_News WHERE storyID NOT LIKE '".strtoupper($xoopsModuleConfig['stationLetters'])."%' AND soundfile != '' ORDER BY publish_date DESC,ID DESC LIMIT ".$xoopsModuleConfig['numOfStories2Show']."";
			mysql_select_db($xoopsModuleConfig['syndicationDB']) or ($error = "Could not select database");
			  $result = mysql_query ($Query, $link) or ($error = "Query failed");
				$_SESSION['myrow'] = array();	
				if ($result){
						while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
							$_SESSION['myrow'][] = $row;
						}
				}
				mysql_free_result($result);
				mysql_close ($link);
				$_SESSION['headerTitle'] = "Southeast News";
				$_SESSION['DB'] = $DB;
			//-------------------------------------------------------------------
	}

	if(!isset($_GET['page'])){
		$_GET['page'] = 1;
	}
	
	//PAGINATE RESULTS IF ANY EXIST 
	if(isset($_SESSION['myrow'])){
		include("include/paginator.php");
		include("include/paginator_html.php");
		$num_rows = count($_SESSION['myrow']);
		$a =& new Paginator_html($_GET['page'],$num_rows);
		$a->set_Limit($xoopsModuleConfig['numOfResultsPerPage']);
		$a->set_Links(20);
		$limit1 = $a->getRange1(); 
		$limit2 = $a->getRange2();
		$myrow = array_slice ($_SESSION['myrow'], $limit1,$limit2);
		if(count ($_SESSION['myrow']) > $xoopsModuleConfig['numOfResultsPerPage']){
			$a->previousNext();
		}
	}
	
	
	
	
		//HEADER FOR S.E. or LOCAL NEWS STORIES
		echo"<table style='margin:0; padding:0;'>
          <tr>
            <td id='centerCcolumn' style='margin:0; padding:0;'>
   				<div class='blockTitle'>".$_SESSION['headerTitle']."</div>
			</td>
		 </tr>
		 </table>";
		 
		function isSyndicated(){
			if($_SESSION['headerTitle'] == "Southeast News"){
				return "true";
			}else{
				return "false";
			}
		}
		

//LOOP THROUGH RESULTS AND DISPLAY LIST OF NEWS ITEMS
if(isset($myrow) && count($myrow) > 0){
	for($i=0; $i < count($myrow); $i++){
	 echo"<div class='news-type' style=' POSITION: relative;  VISIBILITY: visible; text-align:left'>
	 
	  <DIV CLASS='headline-block'id='previewDiv'>";	  
  
		  if($myrow[$i]['image'] == ""){//NO IMAGE
			echo "<DIV CLASS='headline-large' id='headlinePreview'>".$myrow[$i]['headline']."</DIV>
				 <div>".$myrow[$i]['byline']."</div>";
				 if($myrow[$i]['soundfile']){
 					echo"<div id='soundClip".$i."' style='margin-top:2px;'><a href='javascript:void(0);' onClick=\"displayMediaPlayer('".getSoundFile($myrow[$i]['soundfile'])."','soundClip".$i."','flashID".$i."')\"><img style='margin-left:0;  margin-bottom:0' src='".XOOPS_URL."/modules/local_news/images/listen2.gif' alt='Listn Now' /></a></div>";
				}
				echo" <div style='margin-top:5px;margin-bottom:0px;padding-bottom:0px;'>".$myrow[$i]['dateline']." (".date("Y-m-d",$myrow[$i]['publish_date']).") ". $myts->displayTarea($myrow[$i]['tease']); 
					if($myrow[$i]['body']){
						echo " <a href='javascript:void(0);' onClick=\"addBody('bodyHolder$i','moreLink$i'); \" id='moreLink$i'>more...</a><div id='bodyHolder$i' style='display:none;'><br />".trim(html_entity_decode($myrow[$i]['body']))."</div>";
					}
				echo"<div style='padding-top:5px;' CLASS='article-small' >" .getCopyright($myrow[$i]['publish_date'],$myrow[$i]['copyright']). " <br />
			  	   <div style='margin-top:7px;'><a href='javascript:void(0);' onClick=\"emailStory(".$myrow[$i]['ID'].",".isSyndicated().")\"><img alt='Email This Article to a Friend.' style='margin-left:0; margin-bottom:0;' src='".XOOPS_URL."/modules/local_news/images/email.gif' /></a> &nbsp; &nbsp; &nbsp; <a href='javascript:void(0);' onClick=\"printStory(".$myrow[$i]['ID'].",".isSyndicated().")\"><img alt='Print This Article' style='margin-left:0; margin-bottom:0;' src='".XOOPS_URL."/modules/local_news/images/print.gif' /></a> &nbsp; &nbsp; &nbsp; <a href='".getSoundFile($myrow[$i]['soundfile'])."'><img src='images/download.gif' alt='Download this News Story'/></a> &nbsp; &nbsp; &nbsp; <A HREF='".getContactFormURL($myrow[$i]['soundfile'])."/modules/contact_form/'><img src='images/feedback.gif' alt='Submit News Tip' /></A> &nbsp; &nbsp; &nbsp; <A href='".XOOPS_URL."/modules/contact_form/'><img src='images/news_tip.gif' alt='Submit Feedback' /></A></div></div>
				</div>";
			}else{//GOT AN IMAGE
			echo "<DIV CLASS='headline-large' id='headlinePreview'>".$myrow[$i]['headline']."</DIV>
				 <div>".$myrow[$i]['byline']."</div>";
				 if($myrow[$i]['soundfile']){

 					echo"<div id='soundClip".$i."' style='margin-top:2px;'><a href='javascript:void(0);' onClick=\"displayMediaPlayer('".getSoundFile($myrow[$i]['soundfile'])."','soundClip".$i."','flashID".$i."')\"><img style='margin-left:0;  margin-bottom:0' src='".XOOPS_URL."/modules/local_news/images/listen2.gif' alt='Listn Now' /></a></div>";
				}
				echo" <div style='margin-top:5px;margin-bottom:0px;padding-bottom:0px;'>
					 <div style='width:".$myrow[$i]['imageTNwidth']."px;float:left;padding-right:5px;padding-bottom:5px;' >
						<div><a href='".getImageUrl($myrow[$i]['image'])."' TARGET='_blank'> <img src='".getImageUrl($myrow[$i]['image'],true)."' width='".$myrow[$i]['imageTNwidth']."' alt='".$myrow[$i]['caption']."' style='margin-left:0; float:left; margin-right:3px; margin-bottom:0px; border:1px solid #000000;' /></a></div>";
						if($myrow[$i]['credit']){
							echo "<div CLASS='photo-credit' >Image by ".$myrow[$i]['credit']."</div>";
						}	
						if($myrow[$i]['caption']){		
						echo"<div CLASS='photo-caption'>".$myrow[$i]['caption']."</div>";
						}
					echo"</div>
					".$myrow[$i]['dateline']." (".date("Y-m-d",$myrow[$i]['publish_date']).") ". $myts->displayTarea($myrow[$i]['tease']);
					if($myrow[$i]['body']){
						echo " <a href='javascript:void(0);' onClick=\"addBody('bodyHolder$i','moreLink$i'); \" id='moreLink$i'>more...</a><div id='bodyHolder$i' style='display:none;'><br />".trim(html_entity_decode($myrow[$i]['body']))."</div>";
					}
				echo"<div style='padding-top:5px;' CLASS='article-small' >" .getCopyright($myrow[$i]['publish_date'],$myrow[$i]['copyright']). " <br />
			  	   <div style='margin-top:7px;'><a href='javascript:void(0);' onClick=\"emailStory(".$myrow[$i]['ID'].",".isSyndicated().")\"><img alt='Email This Article to a Friend.' style='margin-left:0; margin-bottom:0;' src='".XOOPS_URL."/modules/local_news/images/email.gif' /></a> &nbsp; &nbsp; &nbsp; <a href='javascript:void(0);' onClick=\"printStory(".$myrow[$i]['ID'].",".isSyndicated().")\"><img alt='Print This Article' style='margin-left:0; margin-bottom:0;' src='".XOOPS_URL."/modules/local_news/images/print.gif' /></a> &nbsp; &nbsp; &nbsp; <a href='".getSoundFile($myrow[$i]['soundfile'])."'><img src='images/download.gif' alt='Download this News Story'/></a>&nbsp; &nbsp; &nbsp; <A HREF='".getContactFormURL($myrow[$i]['soundfile'])."/modules/contact_form/'><img src='images/feedback.gif' alt='Submit News Tip' /></A> &nbsp; &nbsp; &nbsp; <A href='".XOOPS_URL."/modules/contact_form/'><img src='images/news_tip.gif' alt='Submit Feedback' /></A></div></div>
				</div>";
			}
	  //END STORY LOGIC
	  //#####################
	  echo"</DIV>
	   </div> <div style='clear:left;border-bottom:1px dotted #999999;margin-top:2px; margin-bottom:10px;'></div>";	
	
	}
	
		if(count ($_SESSION['myrow']) > $xoopsModuleConfig['numOfResultsPerPage']){
			$a->previousNext();
		}
	
 }else{
 	echo "No News Stories Available at this Time.";
 }


}//END FUNCTION



//####################################################################################
//############### FETCH SINGLE STORY FROM ONE OF THE BLOCKS ##########################
//####################################################################################
function fetchStory($ID,$syndicated){
    global $xoopsDB, $xoopsConfig, $xoopsModule, $myts, $xoopsUser, $xoopsModuleConfig, $link;
	$myts =& MyTextSanitizer::getInstance();

	//GET INFO FROM DB
	if(!$syndicated){
			//-------------------------------------------------------------------
			$result = $xoopsDB->query( "SELECT * FROM ".$xoopsDB->prefix("local_news_stories")." WHERE ID = '".$ID."'" ) ;
			if ( $result ) {
				$myrow = $xoopsDB->fetchArray($result);
			}	
			$syndicated = 'false';
			//-------------------------------------------------------------------
	}else{//get from Syndicated DB
			//-------------------------------------------------------------------
			$Query = "SELECT * FROM CA_SE_News WHERE ID = '".$ID."'";
			mysql_select_db($xoopsModuleConfig['syndicationDB']) or ($error = "Could not select database");
			  $result = mysql_query ($Query, $link) or ($error = "Query failed");
	
				if ($result){
					$myrow = mysql_fetch_array($result, MYSQL_ASSOC);
					//$row['headline'];
					}
				mysql_free_result($result);
				mysql_close ($link);
			//-------------------------------------------------------------------
	}
	
	

	

	echo"<div id='preview' class='news-type' style=' POSITION: relative;  VISIBILITY: visible; text-align:left'>
	 
	  <DIV CLASS='headline-block'id='previewDiv'>";
	  
	  

	  
		  if($myrow['image'] == ""){//NO IMAGE
			echo "<DIV CLASS='headline-large' id='headlinePreview'>".$myrow['headline']."</DIV>
				 <div id='bylinePreview'>".$myrow['byline']."</div>";
				 if($myrow['soundfile']){

					echo"<div id='soundClip' style='margin-top:2px;'><!--BEGIN MP3 PLAYER--><object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0' width='300' height='75' id='mp3Player' align='middle'><param name='allowScriptAccess' value='sameDomain' /><param name='movie' value='".XOOPS_URL."/modules/local_news/media/mp3Player.swf' /><param name='quality' value='high' /><param name='bgcolor' value='#ffffff' /><PARAM NAME=FlashVars VALUE='file=".getSoundFile($myrow['soundfile'])."&autoPlayStart=true'><embed src='".XOOPS_URL."/modules/local_news/media/mp3Player.swf' FlashVars='file=".getSoundFile($myrow['soundfile'])."&autoPlayStart=true' quality='high' bgcolor='#ffffff' width='300' height='75' name='mp3Player' align='middle' allowScriptAccess='sameDomain' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' /></object><!--END MP3 PLAYER--></div>";
				}
				echo" <div id='container' style='margin-top:10px;margin-bottom:10px;'>".$myrow['dateline']." (".date("Y-m-d",$myrow['publish_date']).") ". $myts->displayTarea($myrow['tease']); 
					if($myrow['body']){
						echo " <a href='javascript:void(0);' onClick=\"addBody('bodyHolder','moreLink'); \" id='moreLink'>more...</a><div id='bodyHolder' style='display:none;'><br />".trim(html_entity_decode($myrow['body']))."</div>";
					}
				echo"<div id='copy' style='padding-top:5px;' CLASS='article-small' >" .getCopyright($myrow['publish_date'],$myrow['copyright']). "</div>
				</div>";
			}else{//GOT AN IMAGE
			echo "<DIV CLASS='headline-large' id='headlinePreview'>".$myrow['headline']."</DIV>
				 <div id='bylinePreview'>".$myrow['byline']."</div>";
				 if($myrow['soundfile']){
					//OLD - echo"<div id='soundClip' style='margin-top:2px;'><a href='#' onClick=\"displayMediaPlayer('".getSoundFile($myrow['soundfile'])."')\"><img style='margin-left:0;  margin-bottom:0' src='".XOOPS_URL.'/modules/'.$xoopsModule->getVar('dirname')."/images/listen2.gif'></a></div>";
					echo"<div id='soundClip' style='margin-top:2px;'><!--BEGIN MP3 PLAYER--><object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0' width='300' height='75' id='mp3Player' align='middle'><param name='allowScriptAccess' value='sameDomain' /><param name='movie' value='".XOOPS_URL."/modules/local_news/media/mp3Player.swf' /><param name='quality' value='high' /><param name='bgcolor' value='#ffffff' /><PARAM NAME=FlashVars VALUE='file=".getSoundFile($myrow['soundfile'])."&autoPlayStart=true'><embed src='".XOOPS_URL."/modules/local_news/media/mp3Player.swf' FlashVars='file=".getSoundFile($myrow['soundfile'])."&autoPlayStart=true' quality='high' bgcolor='#ffffff' width='300' height='75' name='mp3Player' align='middle' allowScriptAccess='sameDomain' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' /></object><!--END MP3 PLAYER--></div>";
				} 
				echo" <div id='container' style='margin-top:10px;margin-bottom:10px;'>
					 <div style='width:".$myrow['imageTNwidth']."px;float:left;padding-right:5px;padding-bottom:5px;' >
						<div id='image'><a href='".getImageUrl($myrow['image'])."' TARGET='_blank'> <img src='".getImageUrl($myrow['image'],true)."' width='".$myrow['imageTNwidth']."' alt='".$myrow['caption']."' style='margin-left:0; float:left; margin-right:3px; margin-bottom:0px; border:1px solid #000000;' /></a></div>";
						if($myrow['credit']){
							echo "<div id='credit' CLASS='photo-credit' >Image by ".$myrow['credit']."</div>";
						}	
						if($myrow['caption']){		
						echo"<div id='caption' CLASS='photo-caption'>".$myrow['caption']."</div>";
						}
					echo"</div>
					".$myrow['dateline']." (".date("Y-m-d",$myrow['publish_date']).") ". $myts->displayTarea($myrow['tease']);
					if($myrow['body']){
						echo " <a href='javascript:void(0);' onClick=\"addBody('bodyHolder','moreLink'); \" id='moreLink'>more...</a><div id='bodyHolder' style='display:none;'><br />".trim(html_entity_decode($myrow['body']))."</div>";
					}
				echo"<div id='copy' style='padding-top:5px;' CLASS='article-small' >" .getCopyright($myrow['publish_date'],$myrow['copyright']). "</div>
				</div>";
			}
	  //END STORY LOGIC
	  //#####################
  	  echo"<div style='margin-top:7px;'><a href='javascript:void(0);' onClick=\"emailStory(".$myrow['ID'].",".$syndicated.")\"><img alt='Email This Article to a Friend.' style='margin-left:0; margin-bottom:0;' src='".XOOPS_URL."/modules/local_news/images/email.gif' /></a> &nbsp; &nbsp; &nbsp; <a href='javascript:void(0);' onClick=\"printStory(".$myrow['ID'].",".$syndicated.")\"><img alt='Print This Article' style='margin-left:0; margin-bottom:0;' src='".XOOPS_URL."/modules/local_news/images/print.gif' /></a> &nbsp; &nbsp; &nbsp; <a href='".getSoundFile($myrow['soundfile'])."'><img src='images/download.gif' alt='Download this News Story' /></a>&nbsp; &nbsp; &nbsp; <A HREF='".getContactFormURL($myrow['soundfile'])."/modules/contact_form/'><img src='images/feedback.gif' alt='Submit News Tip' /></A> &nbsp; &nbsp; &nbsp; <A href='".XOOPS_URL."/modules/contact_form/'><img src='images/news_tip.gif' alt='Submit Feedback' /></A></div>";
	  echo"</DIV>
	   </div> ";	
	
	
}



include_once XOOPS_ROOT_PATH.'/footer.php';
?>
